﻿using System;
using System.Diagnostics;

namespace RFEngine
{
    public sealed class TimeManager
    {
        #region Fields

        private static bool _Initialized = false;
        private static bool _Paused = false;
        private static float _LastTimeForUpdate;
        private static float _LastUpdateTime;
        private static float _Frequency;

        private static Stopwatch _StopWatch;
        private static Stopwatch _PauseTimer;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the total amount of time the game has been running.
        /// </summary>
        public static float ElapsedGameTime
        {
            get
            {
                if (_Initialized)
                    return (float)(_StopWatch.ElapsedTicks / _Frequency) - (_PauseTimer.ElapsedTicks / _Frequency);

                return 0.0f;
            }
        }

        /// <summary>
        /// Gets the amount of time in seconds of the last update.
        /// </summary>
        public static float LastUpdateTime
        {
            get { return _LastUpdateTime; }
        }

        /// <summary>
        /// Gets the amount of time in seconds of the current update.
        /// </summary>
        public static float CurrentUpdateTime
        {
            get
            {
                if (_Initialized)
                    return ElapsedGameTime - _LastTimeForUpdate;

                return 0.0f;
            }
        }

        #endregion

        #region Methods

        #region Constructor(s)

        /// <summary>
        /// Constructs a new instance of this object.
        /// </summary>
        internal TimeManager()
        {
            _Initialized = true;

            _Frequency = Stopwatch.Frequency;
            _StopWatch = new Stopwatch();
            _PauseTimer = new Stopwatch();

            // EngineServices.OnPause += new Action(OnPause);
            // EngineServices.OnResume += new Action(OnResume);

            _StopWatch.Start();
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        private void OnPause()
        {
            _PauseTimer.Start();
        }

        /// <summary>
        /// 
        /// </summary>
        private void OnResume()
        {
            _PauseTimer.Stop();
        }

        /// <summary>
        /// Method for Updating this instance of the TimeManager.
        /// </summary>
        internal void Update()
        {
            _LastUpdateTime = CurrentUpdateTime;
            _LastTimeForUpdate += _LastUpdateTime;
        }

        #endregion
    }
}
